package com.wn.pettyloan.controller;

import com.wn.pettyloan.dto.UserDto;
import com.wn.pettyloan.entity.User;
import com.wn.pettyloan.service.UserService;
import com.wn.pettyloan.util.Result;
import com.wn.pettyloan.vo.UserVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @program: pettyloan
 * @ClassName UserController
 * @author: 韩旭
 * @create: 2022-12-06 14:26
 **/
@RestController()
@RequestMapping("user")
public class UserController {
    //@Resource
    //private PasswordEncoder passwordEncoder;

    @Resource
    private UserService userService;

    /**
     * @return com.wn.pettyloan.util.Result
     * @Author 韩旭
     * @Description loginDo
     * @Date 14:41 2022/12/6
     * @Param [userDto]
     **/
   @PostMapping("login")
    public Result loginDo(@RequestBody UserDto userDto) {
        User user = userService.login(userDto);
        if (user != null) {
            return Result.ok().data("user", user);
        } else {
            return Result.error();
        }
    }

    /**
     * @return com.wn.pettyloan.util.Result
     * @Author 韩旭
     * @Description selectSelf 查询用户基本信息及标签
     * @Date 11:40 2022/12/7
     * @Param [userDto]
     **/
    @PostMapping("usershow")
    public Result selectSelf(@RequestBody UserDto userDto) {

        UserVo userVo = userService.selectSelf(userDto);
        if (userVo != null) {
            return Result.ok().data("userVo", userVo);
        } else {
            return Result.error();
        }
    }

    @PutMapping("updateUser")
    public Result updateUserNameOrImg(@RequestBody UserDto userDto) {

        userService.updateUserNameOrImg(userDto);

        return Result.ok();

    }

    //@PostMapping("register")
    //public Result registerUser(@RequestBody UserDto userDto) {
    //    userDto.setPassword(passwordEncoder.encode(userDto.getPassword()));
    //    int i = userService.insertUser(userDto);
    //    return i != 0 ? Result.ok() : Result.error("当前用户已被注册");
    //}
}